﻿/*
 * Created by SharpDevelop.
 * User: tdptcj
 * Date: 12/10/2011
 * Time: 17:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

using MetaVideoEditorEvolved.Helper;

namespace MetaVideoEditorEvolved.Control
{
	/// <summary>
	/// Interaction logic for PosterControl.xaml
	/// </summary>
	public partial class PosterControl : UserControl
	{
		private PosterControlVM posterControlVm;
		
		public static readonly DependencyProperty DoThumbnailProperty =
			DependencyProperty.Register("DoThumbnail", typeof(bool), typeof(PosterControl),
			                            new FrameworkPropertyMetadata(true));
		
		public bool DoThumbnail {
			get { return (bool)GetValue(DoThumbnailProperty); }
			set { SetValue(DoThumbnailProperty, value); }
		}
		
		public static readonly DependencyProperty IsReadyProperty =
			DependencyProperty.Register("IsReady", typeof(bool), typeof(PosterControl),
			                            new FrameworkPropertyMetadata(false));
		
		public bool IsReady {
			get { return (bool)GetValue(IsReadyProperty); }
			set { SetValue(IsReadyProperty, value); }
		}
		
		public static readonly DependencyProperty ImageUriProperty =
			DependencyProperty.Register("ImageUri", typeof(string), typeof(PosterControl),
			                            new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(ImageUriChanged)));
		
		public string ImageUri {
			get { return (string)GetValue(ImageUriProperty); }
			set { SetValue(ImageUriProperty, value); }
		}
		
		public static readonly DependencyProperty PosterProperty =
			DependencyProperty.Register("Poster", typeof(UI.Entity.PosterMediaItem), typeof(PosterControl),
			                            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PosterChanged)));
		
		public UI.Entity.PosterMediaItem Poster {
			get { return (UI.Entity.PosterMediaItem)GetValue(PosterProperty); }
			set { SetValue(PosterProperty, value); }
		}
		
		public static readonly DependencyProperty VideoProperty =
			DependencyProperty.Register("Video", typeof(UI.Entity.VideoMediaElement), typeof(PosterControl),
			                            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(VideoChanged)));
		
		public UI.Entity.VideoMediaElement Video {
			get { return (UI.Entity.VideoMediaElement)GetValue(VideoProperty); }
			set { SetValue(VideoProperty, value); }
		}
		public PosterControl()
		{
			InitializeComponent();
			
			this.DataContextChanged+= delegate {
				if (this.posterControlVm!=null) {
					this.posterControlVm.IsReinitializing = true;
					this.posterControlVm.IsInError = false;
					this.posterControlVm.Video = null;
					this.posterControlVm.Poster = null;
					this.posterControlVm.ImagePath = string.Empty;
					this.posterControlVm.ImageRatio = string.Empty;
					this.posterControlVm.ImageWidth = 0;
					this.posterControlVm.ImageHeight = 0;
					this.posterControlVm.IsReinitializing = false;					
					this.posterControlVm.DoThumbnail = true;
					this.IsReady = false;
				}
			};
			
			this.Loaded+= delegate {
				if (this.posterControlVm==null) {
					this.posterControlVm = this.Resources["posterControlViewModel"] as PosterControlVM;
				}
				
				this.posterControlVm.DoThumbnail = this.DoThumbnail;
				
				this.posterControlVm.ActualHeight = this.ActualHeight;
				this.posterControlVm.ActualWidth = this.ActualWidth;				
				
				this.posterControlVm.ImageObject = this.MiniPoster;
				this.posterControlVm.ZoomImageObject = this.FullPoster;
				
				this.posterControlVm.ZoomImageObject.Loaded+= delegate { 
					this.posterControlVm.DownloadZoomImage();
				};
				
				this.posterControlVm.Poster = this.Poster;							
				
				this.posterControlVm.Video = this.Video;
				
				this.posterControlVm.ImagePath = this.ImageUri;
				
				this.IsReady = true;
			};
		}
		
//		protected override Size MeasureOverride(Size constraint)
//		{
//			var size = new Size();
//			
//			var path = this.Poster == null ? this.ImageUri : this.Poster.Image;
//			
//			if (!File.Exists(path)) {
//				this.MiniPoster.Stretch = Stretch.Uniform;
//				return constraint;
//			}
//			
//			using(var file = new FileStream(path,FileMode.Open))
//			{
//				var imgSize = System.Drawing.Image.FromStream(file,false, false).Size;				
//				
//				// compute ratio
//				bool takeWidth = false;
//				
//				if (double.IsInfinity(constraint.Width) || constraint.Width == 0) {
//					takeWidth = false;
//				}
//				else
//				{
//					if (double.IsInfinity(constraint.Height) || constraint.Height == 0) {
//						takeWidth = true;
//					}
//					else
//					{
//						if (imgSize.Width>constraint.Width && imgSize.Height<constraint.Height) {
//							takeWidth = true;
//						}
//						else
//						{
//							if (imgSize.Height>constraint.Height && imgSize.Width<constraint.Width) {
//								takeWidth = false;
//							}
//							else
//							{
//								if (imgSize.Width<imgSize.Height) {
//									takeWidth = false;
//								}
//								else
//								{
//									takeWidth = true;
//								}
//							}
//						}
//					}
//				}
//				
//				if (takeWidth) {
//					if (imgSize.Width<constraint.Width) {
//						size = new System.Windows.Size(imgSize.Width, imgSize.Height);
//					}
//					else
//					{
//						var multiplicator = constraint.Width / imgSize.Width;
//						var height = imgSize.Height * multiplicator;
//						size = new System.Windows.Size(constraint.Width, height);
//					}
//				}
//				else
//				{
//					if (imgSize.Height<constraint.Height) {
//						size = new System.Windows.Size(imgSize.Width, imgSize.Height);
//					}
//					else
//					{
//						var multiplicator = constraint.Height / imgSize.Height;
//						var width = imgSize.Width * multiplicator;
//						size = new System.Windows.Size(width, constraint.Height);
//					}
//				}
//				
//				return size;
//			}					
//		}
		
		private static void PosterChanged(DependencyObject control, DependencyPropertyChangedEventArgs args)
		{
			var realControl = (PosterControl)control;
			if (realControl.posterControlVm==null) {
				realControl.posterControlVm = realControl.Resources["posterControlViewModel"] as PosterControlVM;
			}		

			realControl.posterControlVm.Poster = args.NewValue as UI.Entity.PosterMediaItem;		
		}
		
		private static void VideoChanged(DependencyObject control, DependencyPropertyChangedEventArgs args)
		{
//			var realControl = (PosterControl)control;
//			if (realControl.posterControlVm==null) {
//				realControl.posterControlVm = realControl.Resources["posterControlViewModel"] as PosterControlVM;
//			}
//
//			realControl.posterControlVm.ImageObject = realControl.MiniPoster;
//			realControl.posterControlVm.ZoomImageObject = realControl.FullPoster;
//
//			realControl.posterControlVm.Video = args.NewValue as UI.Entity.VideoMediaElement;
		}
		
		private static void ImageUriChanged(DependencyObject control, DependencyPropertyChangedEventArgs args)
		{
//			var realControl = (PosterControl)control;
//			if (realControl.posterControlVm==null) {
//				realControl.posterControlVm = realControl.Resources["posterControlViewModel"] as PosterControlVM;
//			}
//
//			realControl.posterControlVm.ImageObject = realControl.MiniPoster;
//			realControl.posterControlVm.ZoomImageObject = realControl.FullPoster;
//
//			realControl.posterControlVm.ImagePath = args.NewValue as string;
//
//			if (!string.IsNullOrEmpty(realControl.posterControlVm.ImagePath)) {
//				realControl.IsReady = true;
//			}
		}
	}
}